import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/common/su_dialog.dart';
import 'package:love_app/common/su_icon.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/routes/route.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

ShareProvider _shareProvider;
class MyBottomSheet extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    _shareProvider = Provider.of<ShareProvider>(context, listen: false);
    return Container(
      height: Su().h(356),
      child: Column(
        children: <Widget>[
          _title(),
          _allIcon(),
          _lineWidget(),
          _cancel(),
        ],
      ),
    );
  }

  // 标题
  Widget _title() {
    return Padding(
      padding: EdgeInsets.only(top: Su().h(23)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            "邀请另一半和你共同使用",
            style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 30),
          ),
        ],
      ),
    );
  }

  // 中间图标
  Widget _allIcon() {
    return Padding(
      padding: EdgeInsets.only(top: Su().h(41)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          _qqIcon(),
          _messageIcon(),
          _wxIcon(),
        ],
      ),
    );
  }

  Widget _qqIcon() {
    return Column(
      children: <Widget>[
        IconButton(
          onPressed: () async {
            await _openApp("mqq://");
          },
          padding: EdgeInsets.all(0),
          icon: Icon(
            SuIcon.qq,
            size: Su().w(80),
            color: Color.fromRGBO(35, 160, 240, 1),
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[Text('QQ')],
        ),
      ],
    );
  }

  Widget _messageIcon() {
    return Column(
      children: <Widget>[
        IconButton(
          onPressed: () async {
            await _openApp("sms://15813562346");
          },
          padding: EdgeInsets.all(0),
          icon: Icon(
            SuIcon.message,
            size: Su().w(80),
            color: Color.fromRGBO(113, 164, 216, 1),
          ),
        ),
        Text('短信')
      ],
    );
  }

  Widget _wxIcon() {
    return Column(
      children: <Widget>[
        IconButton(
          onPressed: () async {
            await _openApp("weixin://");
          },
          padding: EdgeInsets.all(0),
          icon: Icon(
            SuIcon.wx,
            size: Su().w(80),
            color: Color.fromRGBO(40, 184, 54, 1),
          ),
        ),
        Text('微信')
      ],
    );
  }

  // 一条线
  Widget _lineWidget(){
    return Container(
      color: Color.fromRGBO(224, 224, 224, 1),
      height: Su().h(2),
      margin: EdgeInsets.only(top: Su().h(27)),
    );
  }

  // 取消
  Widget _cancel(){
    return InkWell(
      onTap: (){
        MyRouter.navigatorKey.currentState.pop();
      },
      child: Container(
        child: Text('取消', style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 30),),
        alignment: Alignment.center,
        height: Su().h(70),
      ),
    );
  }

  // 通过url_launcher打开应用
  Future<void> _openApp(String url)async{
    if(await canLaunch(url)){
      _saveClipboard();
      await launch(url);
    }else{
      SuDialog.showToast(
        '找不到该应用',
        bgColor: _shareProvider.textColor,
      );
    }
  }

  void _saveClipboard(){
    const String = '邀请您成为情侣';
    Clipboard.setData(ClipboardData(text: String));
    SuDialog.showToast(
        '复制成功',
        bgColor: _shareProvider.textColor,
      );
  }
}
